package com.whfc.fse.queue;

import com.alibaba.fastjson.JSON;
import com.whfc.common.constant.QueueConst;
import com.whfc.fse.dao.FseCraneDataMapper;
import com.whfc.fse.dao.FseCraneMapper;
import com.whfc.fse.dto.FseCraneParamDTO;
import com.whfc.fse.entity.FseCrane;
import com.whfc.fse.entity.FseCraneData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 处理硬件上传的 塔机参数
 *
 * @author sun_guodong
 * @date 2020-05-26
 */
@Component
@RabbitListener(queuesToDeclare = {@Queue(name = QueueConst.FSE_CRANE_PARAM_DATA)}, concurrency = "1-2",containerFactory = "baseFactory")
public class FseCraneParamDataQueueListener {

    private static final Logger logger = LoggerFactory.getLogger(FseCraneParamDataQueueListener.class);

    @Autowired
    private FseCraneDataMapper fseCraneDataMapper;

    @Autowired
    private FseCraneMapper fseCraneMapper;

    @RabbitHandler
    public void handle(String msg) {
        try {
            logger.info("消息消费者处理塔机参数,msg:{}", msg);
            FseCraneParamDTO fseCraneParamDTO = JSON.parseObject(msg, FseCraneParamDTO.class);
            String platform = fseCraneParamDTO.getPlatform();
            String sn = fseCraneParamDTO.getSn();
            FseCrane fseCrane = fseCraneMapper.selectByPlatformAndSn(platform, sn);
            if (fseCrane == null) {
                return;
            }
            FseCraneData fseCranedata = fseCraneDataMapper.selectByCraneId(fseCrane.getId());
            if (fseCranedata == null) {
                //没找到当前设备信息，跳过
                return;
            }
            BeanUtils.copyProperties(fseCraneParamDTO, fseCranedata);
            fseCraneDataMapper.updateByPrimaryKeySelective(fseCranedata);
        } catch (Exception e) {
            logger.error("消息消费者处理塔机参数失败", e);
        }

    }
}
